package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.app.dao.BsDepartDao;
import io.renren.modules.app.dao.BsPostDao;
import io.renren.modules.app.entity.BsDepartEntity;
import io.renren.modules.app.entity.BsPostEntity;
import io.renren.modules.app.service.BsPostService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;



@Service("bsPostService")
public class BsPostServiceImpl extends ServiceImpl<BsPostDao, BsPostEntity> implements BsPostService {

    @Resource
    private BsPostDao bsPostDao ;

    @Resource
    private BsDepartDao bsDepartDao ;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<BsPostEntity> wrapper = new LambdaQueryWrapper<>();

        if (params.get("departId") != null && !params.get("departId").equals("")){
            wrapper.eq(BsPostEntity::getDepartId,params.get("departId"));
        }
        if (params.get("postName") != null && !params.get("postName").equals("")){
            wrapper.like(BsPostEntity::getPostName,params.get("postName"));
        }
        IPage<BsPostEntity> page = this.page(
                new Query<BsPostEntity>().getPage(params),
                wrapper
        );

        for (BsPostEntity record : page.getRecords()) {
            Long departId = record.getDepartId(); //部门编号
            BsDepartEntity bsDepartEntity = bsDepartDao.selectById(departId);
            record.setDepartName(bsDepartEntity.getDepartName());
        }

        return new PageUtils(page);
    }

    @Override
    public List<BsDepartEntity> getDepart() {

        List<BsDepartEntity> entities = bsDepartDao.selectList(null);
        return entities ;
    }

}
